<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card class="box-card">
      <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      <!-- 弹出添加分类对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addDialogVisible"
        width="50%"
      > <el-form
          :model="AddForm"
          :rules="Addcaterules"
          ref="Addcateref"
          label-width="100px"
          class="demo-ruleForm"
          @close="addCateClosed"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="AddForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类" prop="cat_pid">
            <el-cascader
              v-model="selectedKeys"
              :options="parentCateList"
              :props="propsc"
              @change="handleChange"
              clearable></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addFormCate">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹出编辑按钮 -->
       <el-dialog
        title="编辑分类"
        :visible.sync="editDialogVisible"
        width="50%"
      > <el-form
          :model="EidtForm"
          :rules="Eidtcaterules"
          ref="Eidtcateref"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="EidtForm.cat_name"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editpro">确 定</el-button>
        </span>
      </el-dialog>
        <tree-table style="margin-top:20px" :data="catList" :columns="columns"
        show-index index-text="#" border :show-row-hover="false"
        :expand-type="false" :selection-type="false" >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning" v-if="scope.row.cat_level === 2">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editCate(scope.row.cat_id)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="removerCate(scope.row.cat_id)">删除</el-button>
        </template></tree-table>
        <!-- 分页展示-->
          <el-pagination
           @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[3, 5, 10, 15]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 为table指定列
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      },
      {
        label: '是否有效',
        prop: '',
        type: 'template',
        template: 'isok'
      }, {
        label: '排序',
        prop: '',
        type: 'template',
        template: 'order'
      }, {
        label: '操作',
        type: 'template',
        template: 'opt'
      }],
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 定义中条数
      total: 0,
      // 商品列表
      catList: [],
      addDialogVisible: false,
      // 添加分类列表
      AddForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 定义表单验证
      Addcaterules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 编辑表单验证
      Eidtcaterules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 保存1，2级分类列表
      parentCateList: [],
      propsc: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      // 绑定用户选择的值
      selectedKeys: [],
      editDialogVisible: false,
      // 接收获取编辑
      EidtForm: []
    }
  },
  created() {
    this.getcatList()
  },
  methods: {
    async getcatList() {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      // console.log(res.data)
      this.catList = res.data.result
      this.total = res.data.total
    },
    // 删除分类控件
    async removerCate(Cate) {
      // console.log(Cate)
      const confilmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '删除',
        {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消删除',
          type: 'warning'
        }
      ).catch(err => err)
      if (confilmResult !== 'confirm') {
        return this.$message.error('你取消了删除！')
      }
      const { data: res } = await this.$http.delete('categories/' + Cate)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功！')
      this.getcatList()
    },
    // 添加分类控件
    showAddCateDialog() {
      this.getcascader()
      this.addDialogVisible = true
    },
    async getcascader() {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类列表失败！')
      }
      console.log(res.data)
      this.parentCateList = res.data
    },
    handleChange() {
      console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        // 则将数组中的最后一项设置为父级分类
        this.AddForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        //  level也要跟着发生变化
        this.AddForm.cat_level = this.selectedKeys.length
        this.$message.success('已选择')
      } else {
        this.AddForm.cat_pid = 0
        this.AddForm.cat_level = 0
        this.$message.error('未选中')
      }
    },
    // 重置表单
    addCateClosed() {
      this.$refs.Addcateref.resetFields()
      this.selectedKeys = []
      this.AddForm.cat_pid = 0
      this.AddForm.cat_level = 0
    },
    // 获取商品分类列表父级
    addFormCate() {
      console.log(this.AddForm)
      this.$refs.Addcateref.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.AddForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加失败！')
        }
        this.$message.success('添加成功')
        this.addDialogVisible = false
        this.getcatList()
      })
    },
    // 分页控件
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum
      this.getcatList()
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getcatList()
    },
    // 编辑
    async editCate(id) {
      // console.log(id)
      this.editDialogVisible = true
      // 查询分类
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('根据id获取失败')
      }
      // console.log(res.data)
      this.EidtForm = res.data
    },
    // 确认修改
    editpro() {
      this.$refs.Eidtcateref.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('categories/' + this.EidtForm.cat_id, {
          cat_name: this.EidtForm.cat_name
        })
        // console.log(res.data)
        if (res.meta.status !== 200) {
          return this.$message.error('编辑失败！')
        }
        this.$message.success('编辑成功！')
        this.editDialogVisible = false
        this.getcatList()
      })
    }
  }
}
</script>

<style>
</style>
